<?php

class AdminGiftController extends AdminController
{
  public function listAction()
  {
    $giftModel = $this->getModel('gift');
    if ($this->req->isPost()) {
     $image = $this->req->getPostTrim('image');
     $name = $this->req->getPostTrim('name');
     $type = $this->req->getPostTrim('type');
     $flowers = intval('0' . $this->req->getPostTrim('flowers'));
     $amount = intval('0' . $this->req->getPostTrim('amount'));
     $coupon = $this->req->getPostTrim('coupon');
     $status = $this->req->getPostTrim('status') == Gift::IS_ONLINE ? Gift::IS_ONLINE: Gift::IS_OFFLINE;
     if ($type == 'product') {
       $type = Gift::IS_PRODUCT;
      } else if ($type == 'coupon') {
       $type = Gift::IS_COUPON;
     } else {
       $this->addErrorMessage('参数错误！！');
       $this->gotoUrl('admin/gift/list', true);
     }
     if ( hasEmpty([$image, $flowers, $name]) || ($type == Gift::IS_PRODUCT && empty($amount)) || ($type == Gift::IS_COUPON && empty($coupon))) {
       $this->addErrorMessage('参数错误！！');
       $this->gotoUrl('admin/gift/list', true);
     } else if ($giftModel->createGift([
       'user_id' => $this->account->user_id,
       'name' => $name, 'image' => $image,
       'type' => $type, 'status' => $status,
       'amount' => $amount, 'flowers' => $flowers,
       'data' => ['coupon' => $coupon]
     ])) {
       $this->addSuccessMessage('添加礼品[' . $name . ']成功！！');
     } else {
       $this->addErrorMessage('添加礼品[' . $name . ']失败，请重新添加！！');
     }
     $this->gotoUrl('admin/gift/list', true);
    }
    $view = $this->getView();
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $rows = 15;
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
      'type' => trim($this->req->getQuery('type', '')),
    ];
    $view = $this->getView();
    $list = $giftModel->getGiftsList($query, $page, $rows);
    $count = $giftModel->getGiftsCount($query);
    $view->assign([
     'query' => $query,
     'list' => $list, 'count' => $count,
     'page' => $page, 'rows' => $rows
   ]);
    $view->display('admin/gift/list.phtml');
  }

  public function giftUpdatedAction($op, $giftid)
  {
    $giftModel = $this->getModel('gift');
    if (!($gift = $giftModel->getGift($giftid))) {
      $this->addErrorMessage('参数错误！！');
      $this->gotoUrl('admin/gift/list', true);
    } else if ($op == 'delete') {
      if ($gift->delete()) {
        $this->addSuccessMessage('删除礼品[' . $gift->name .  '] 成功');
      } else {
        $this->addErrorMessage('删除礼品[' . $gift->name .  '] 失败,该礼品已有兑换记录');
      }
    } else if ($op == 'online') {
      $gift->status = Gift::IS_ONLINE;
      if ($gift->update()) {
        $this->addSuccessMessage('礼品[' . $gift->name.  '] 上架成功');
      } else {
        $this->addErrorMessage('礼品[' . $gift->name.  '] 上架失败！！');
      }
    } else if ($op == 'offline') {
      $gift->status = Gift::IS_OFFLINE;
      if ($gift->update()) {
        $this->addSuccessMessage('礼品[' . $gift->name.  '] 下架成功');
      } else {
        $this->addErrorMessage('礼品[' . $gift->name.  '] 下架失败！！');
      }
    }
    $this->gotoUrl('admin/gift/list', true);
  }

  public function exchangeAction()
  {
    if ($this->req->getQueryTrim('action') == 'download' ) {
      return $this->exchangeDownloadAction();
    }
    $giftModel = $this->getModel('gift');
    $view = $this->getView();
    $page = max(intval($this->req->getQuery('page', 1)), 1);
    $rows = 15;
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
      'type' => trim($this->req->getQuery('type', '')),
    ];
    $view = $this->getView();
    $list = $giftModel->getPlayerGiftsList($query, $page, $rows);
    $count = $giftModel->getPlayerGiftsCount($query);
    $view->assign([
     'query' => $query,
     'list' => $list, 'count' => $count,
     'page' => $page, 'rows' => $rows
    ]);
    $view->display('admin/gift/exchange.phtml');
  }

  public function exchangeDownloadAction()
  {
    header('Content-type:application/vnd.ms-excel');
    header('Content-Disposition:attachment;filename='. date('Y-m-d_G-i-s', time()) . '_礼品兑换.xls');
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf8" />';
    echo '<table width="200" border="1">';
    echo '<tr><td>' . join('</td><td>', [
      '微信名', '姓名', '电话', '地址', '礼品', '申请时间', '状态'
    ]) . '</td></tr>';
    $giftModel = $this->getModel('gift');
    $query = [
      'search' => trim($this->req->getQuery('search', '')),
      'status' => trim($this->req->getQuery('status', '')),
      'type' => 1,
    ];
    $rows = 100;
    $page = 1;
    do {
      foreach (($list = $giftModel->getPlayerGiftsList($query, $page++, $rows)) as $playerGift) {
        echo '<tr><td>' . join('</td><td style="vnd.ms-excel.numberformat:@ ">', [
          $playerGift->nickname, $playerGift->data['realname'], $playerGift->data['phone'],
          $playerGift->data['address'], $playerGift->data['name'], date('Y-m-d G:i:s', $playerGift->created),
          $playerGift->status == PlayerGift::IS_CREATE ? '未发货': '已发货'
        ]) . '</td></tr>';
      }
    } while( count($list) == $rows);
    echo '</table>';

  }

  public function exchangeUpdatedAction($op, $giftid)
  {
    $giftModel = $this->getModel('gift');
    if (!($playerGift = $giftModel->getPlayerGift($giftid))) {
      $this->addErrorMessage('参数错误！！');
    } else if ($op == 'send') {
      $playerGift->status = PlayerGift::IS_SEND;
      if ($playerGift->update()) {
        $this->addSuccessMessage('礼品[' . $playerGift->data['name'].  '] 已成功发货给[' . $playerGift->data['realname'] . ']' );
      } else {
        $this->addErrorMessage('发货失败，请重试！！');
      }
    }
    $this->gotoUrl('admin/gift/exchange', true);
  }

}
